package com.saxon.springcloud.controller;

import com.netflix.discovery.converters.Auto;
import com.saxon.springcloud.pojo.Dept;
import com.saxon.springcloud.service.DeptService;
import com.saxon.springcloud.service.DeptServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptCotroller {
    @Autowired
    DeptService deptService;

    @Autowired
    DiscoveryClient client;


    @PostMapping ("/insert")
    @ResponseBody
    public int insertDept (Dept dept) {
        return deptService.insertDept (dept);
    }

    @GetMapping ("/get/{id}")
    @ResponseBody
    public Dept queryDeptById (@PathVariable ("id") long id) {
        return deptService.queryDeptById (id);
    }

    @GetMapping ("/getAll")
    @ResponseBody
    public List<Dept> queryAllDept () {
        return deptService.queryAllDept ();
    }


    @GetMapping("/discovery")
    public Object discovery(){
        List<ServiceInstance> saxon = client.getInstances ("SAXON");
        for (ServiceInstance serviceInstance : saxon) {
            System.out.println ("serviceInstance=====>"+serviceInstance.getHost ());
            System.out.println ("serviceInstance=====>"+serviceInstance.getInstanceId ());
            System.out.println ("serviceInstance=====>"+serviceInstance.getServiceId ());
            System.out.println ("serviceInstance=====>"+serviceInstance.getPort ());
            System.out.println ("serviceInstance=====>"+serviceInstance.getUri ());
            System.out.println ("serviceInstance=====>"+serviceInstance.getMetadata ());
        }
        return this.client;
    }
}
